var p1 = new Promise((resolve, reject) => {
	resolve('promise ' +  Math.random())
})


var promises = new Array(10)
for(var i = 0; i < 10 ; i++) {
	promises[i] =  new Promise((resolve, reject) => {
		resolve(`p${i} ` +  (Math.random() + '').slice(-5))
	})
}


// 证明微任务 优先与宏任务执行
function test() {

	p1.then(value => {
		var i = 0;
		console.log('p1', value)
		promises[i++].then(vv => {
			console.log(`p${i+1}`, vv)
			promises[i++].then(vv => {
				console.log(`p${i+1}`, vv)
				promises[i++].then(vv => {
					console.log(`p${i+1}`, vv)
					promises[i++].then(vv => {
						console.log(`p${i+1}`, vv)
						promises[i++].then(vv => {
							console.log(`p${i+1}`, vv)
						})
					})
				})
			})
		})
	})

	setTimeout(() => {
		console.log('-----setTimeout------\n')
	})
}


/**
 * 可以证明微任务优先宏任务执行的(setTimeout)
 * @param  {[type]} i [description]
 * @return {[type]}   [description]
 */
function test2(i) {
	if(i >= promises.length) return ;
	promises[i].then(vv => {
		for(var a=0; a < 100000000;) a++ ;
		console.log(`i = ${i++}`, vv)
		test2(i)
	})
}

test2(0);
setTimeout(() => {
	console.log('------setTimeout test2 ------\n')
})
